package com.zwu.controller;

import com.github.pagehelper.PageInfo;
import com.sun.deploy.security.MSCryptoDSASignature;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import com.zwu.config.JwtToken;
import com.zwu.pojo.Result;
import com.zwu.pojo.Student;
import com.zwu.pojo.Tip;
import com.zwu.pojo.User;
import com.zwu.service.StudentService;
import com.zwu.service.TipService;
import com.zwu.service.UserService;
import com.zwu.util.JwtUtil;
import com.zwu.util.Md5Util;
import com.zwu.util.ResultVoUtil;
import com.zwu.vo.ResultVo;
import com.zwu.vo.UserVo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */

@RestController
@RequestMapping("/admin")
public class AdminController {
    static String token;
    @Autowired
    HttpSession httpSession;
    @Autowired
    StudentService studentService;
    @Autowired
    UserService userService;
    @Autowired
    TipService tipService;
    @JwtToken
    @GetMapping("/findAll")
    public ResultVo findAll(){
        PageInfo<Student> students = studentService.findAllByPage(1,10);
        return ResultVoUtil.success(students);
    }
    @JwtToken
    @GetMapping("/findPage/{val}")
    public ResultVo findPage(@PathVariable Integer val){
        PageInfo<Student> student = studentService.findAllByPage(val,10);
        return ResultVoUtil.success(student);
    }

    @PostMapping("/login")
    public ResultVo login(@RequestBody User user){

        Md5Util md5=new Md5Util();
        boolean checkpassword=false;
        if (user.getPassword()==""){
            return ResultVoUtil.fail("没有输入密码");
        }
        try {
            checkpassword = md5.checkpassword(user.getPassword(), userService.findPassword(user.getName()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (checkpassword==true){

            String sign = JwtUtil.sign(user.getPassword());


            return ResultVoUtil.success(sign);
        }else {
            return ResultVoUtil.fail("登陆失败");
        }

    }
    @GetMapping("/time")
    public ResultVo getTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return ResultVoUtil.success(df.format(new Date()));
    }
    @JwtToken
    @PostMapping("/import")
    public ResultVo excelImport(@RequestParam(value="file") MultipartFile file, HttpSession session){
        String fileName = file.getOriginalFilename();
        Result result=new Result(1,"成功");
        try{
            result = studentService.add(file);
        }catch (Exception e ){
            e.printStackTrace();
        }
        if (result.getResult() > 0 ){
            return  ResultVoUtil.success(result);
        }else{
            String msg = result.getMsg();
            return  ResultVoUtil.fail(result);
        }
    }
    @JwtToken
    @PostMapping("/findStudent")
    public ResultVo findStudent(@RequestBody Student student){
        return ResultVoUtil.success(studentService.findStudent(student.getIdentify(),student.getTime()));
    }
    @JwtToken
    @GetMapping("/deleteStudent/{id}")
    public ResultVo deleteStudent(@PathVariable Integer id){
        studentService.deleteStudent(id);
        System.out.println(1);
        return ResultVoUtil.success(1);
    }
    @JwtToken
    @GetMapping("/update/{id}")
    public ResultVo updateStudent(@PathVariable Integer id){
        Student student = studentService.findStudentById(id);
        return ResultVoUtil.success(student);
    }
    @JwtToken
    @PostMapping("/update")
    public ResultVo updateStudent(@RequestBody Student student){
        studentService.updateStudent(student);
        return ResultVoUtil.success(1);
    }
    @JwtToken
    @PostMapping("/updatePassword")
    public ResultVo updatePassword(@RequestBody UserVo userVo){
        Md5Util md5=new Md5Util();
        String s= "123456";
        if (userVo.getNewPassword()==""){
            return ResultVoUtil.fail("新密码不能为空");
        }
        boolean flag=false;
        try {
            flag = md5.checkpassword(userVo.getOldPassword(), userService.findPassword("admin"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (!flag){
            return  ResultVoUtil.fail("原密码错误");
        }else {
            try {
                 s = md5.EncoderByMd5(userVo.getNewPassword());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            userService.updateUser(s);
            return ResultVoUtil.success("成功");
        }
    }
    @JwtToken
    @PostMapping("/addStudent")
    public ResultVo addStudent(@RequestBody Student student){
        Result result = studentService.addStudent(student);
        if (result.getResult()==0){
            return ResultVoUtil.fail(result.getMsg());
        }else {
            return ResultVoUtil.success("成功");
        }
    }
    @JwtToken
    @GetMapping("/total")
    public ResultVo total(){
        return ResultVoUtil.success(studentService.count());
    }
    @JwtToken
    @GetMapping("/findSuccessTip")
    public ResultVo findSuccessTip(){
        return ResultVoUtil.success(tipService.findSuccess());
    }
    @JwtToken
    @PostMapping("/updateMessage")
    public ResultVo updateMessage(@RequestBody Tip tip){
        tipService.update(tip);
        return ResultVoUtil.success("修改成功");
    }
    @JwtToken
    @GetMapping("/findFailTip")
    public ResultVo findFailTip(){
        return ResultVoUtil.success(tipService.findFail());
    }
}
